﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace VisualCardPrinter_NPPrinter_New
{
    public class RCP
    {

        public static int 串口号 { get; set; }

        public static int 波特率 { get; set; }

        /// <summary>
        /// 打开串口
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int CommOpen(int port, int bandrate);
        /// <summary>
        /// 关闭串口
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int CommClose(int m_hCom);
        /// <summary>
        /// 机器序列号查询
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int RequestSerialNumber(int m_hCom, byte[] res, int waitTime);

        /// <summary>
        /// 机器打印次数
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int RequestPrintCount(int m_hCom, byte[] res, int waitTime);

        /// <summary>
        /// 进卡
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int CardInOver(int m_hCom, int waitTime);
        
        /// <summary>
        /// 弹卡
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int CardInFront(int m_hCom, int waitTime);

        /// <summary>
        /// 出卡
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int Dischange(int m_hCom, int mode, int waitTime);


        /// <summary>
        /// 复位 
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int Reset(int m_hCom, int waitTime);

        /// <summary>
        /// 擦除打印和弹卡函数
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int ErasePrintDischarge(int m_hCom, int dischargeMode, int eraseMode, int printMode, int waitTime);


        /// <summary>
        /// 发送文本
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int PrintCharacterDataSetting(int m_hCom, int mode, int x, int y, StringBuilder data, int dataLen, int waitTime);

        /// <summary>
        /// 块图像数据发送数据缓冲区
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int PrintImageDataSetting(int m_hCom, int x, int y, int ImageL, StringBuilder data, int dataLen, int waitTime);


        /// <summary>
        /// 直接根据路径发送图像缓存
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int PrintImageDataSetting_byfile(int hCom, int imgx, int imgy, StringBuilder imgFile, int waitTime);

        /// <summary>
        /// 清除缓存
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int PrintBufferClear(int m_hCom, int waitTime);


        ///// <summary>
        ///// 查询机器状态
        ///// </summary>
        //[DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        //public unsafe extern static int _stdcallRequestStatus(int m_hCom, char* res, int waitTime);

        #region IC卡操作
        /// <summary>
        /// 寻卡并返回序列号
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int RfidCardRequestAndReadNum(int m_hCom, byte[] code, int Waittime);

        /// <summary>
        /// 寻卡并修改密码
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int RfidCardRequestAndWritePassword(int m_hCom, byte sectornum, byte keymodel, StringBuilder key, byte nkeymodel, StringBuilder newkey, int Waittime);

        /// <summary>
        /// 寻卡并读卡扇区
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int RfidCardRequestAndReadDataSector(int m_hCom, byte sectornum, byte keymodel, StringBuilder key, byte[] code, int Waittime);
        
        
        //寻卡并读卡
        //extern "C" _declspec(dllexport) int _stdcall RfidCardRequestAndReadData(HANDLE m_hCom,char blocknum,char keymodel,char* key,char* code,int Waittime);
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int RfidCardRequestAndReadData(int m_hCom, byte sectornum, byte keymodel, StringBuilder key, byte[] code, int Waittime);
      
        /// <summary>
        /// 寻卡并写卡
        /// </summary>
        [DllImport("RCPV11_VCDLL.dll", SetLastError = true)]
        public extern static int RfidCardRequestAndWriteData(int m_hCom, byte blocknum, byte keymodel, StringBuilder key, StringBuilder data, int Waittime);
        #endregion



        #region 字符长度修正
        /// <summary>
        /// 由于中文汉字占位的问题导致
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static int 修正字符长度(string text)
        {
            if (text.Length == 0) return 0;
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            byte[] s = ascii.GetBytes(text);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }
            }

            return tempLen;
        }
        #endregion
    }
}
